app.controller("expressionCtrl", ['$scope', '$modal', '$rootScope','FileUploader', 'lodash', '$http', function ($scope, $modal, $rootScope, FileUploader, lodash, $http){
    $rootScope.initBreadcrumb($scope.breadcrumb);
    $scope.breadcrumb.push("原型表达式");
    
    $scope.info = {
        expression      : "",
        tableContent    : "table\n[],出口报关指令单号|lsssss,出口报关服务单号|l|u,物流单号,,,,,,,,,,,,,,,,,,,,,,,,\n[],CK01000184879,D010101000040946,C030101000027326\n[], CK01000184879,D010101000040946,C030101000027326",
        tabTableContent : "tab-table\n全部   ,             等        待        清          关  |            v           ,       清关完成--创建,111111111111111,22222222222222222,,,,,,,,,,,,,,,,,,,,,,,,\n[],            出口报关指令单号     |          l, 出口    报关服务单号|l |          u,物流单号,,,,,,,,,,,,,,,,,,,,,\n[], CK01000184879,D010101000040946,C030101000027326,\n[], CK01000184879,D010101000040946,C030101000027326",
        formContent     : "form\ncombo-select:派               送        指令单/xxx指         令单                 |         u,,,,,,,,,,,,,,,,,,,\nselect:是否           异常            ,            请选择         /          是        /             否\ninput:         客户\ndate:           日          期              *          ;                 必                  须                输入,日期小于当天\ndate-to: 起止\ncustomer:          某控件,              请参考原型\nsearch-button:            查询",
        textFormContent : "text-form\nname*,   文本框 ;      i nfo1/info2/info3   |       u\nname*,文本框;info1/info2/info3\nname*,文本框;info1/info2/info3",
        flowContent     : "flow\n11    11  |    u\n2   22\n33   3"
    };
    $scope.isSave = false;
    $scope.selectedItem = "default";

    //保存原型表达式
    $scope.saveExpression = function() {
        if($scope.info.expression == "") {
            $rootScope.toasterErrorInfo("数据不能为空！");
            return false;
        }
        $scope.isSave = true;

        var splitByEnd = $scope.info.expression.split("End\n");
        var resultArray = [];
        angular.forEach(splitByEnd, function (trans, index) {
            var currentType = lodash.trim(trans.split("\n")[0]);
            if (currentType != "tab-table" && currentType != "form" && currentType != "text-form" && currentType != "flow" && currentType != "table") {
                currentType = "other";
            }
            resultArray.push({"type": currentType, "data": trans});
        });
        $scope.expressionData = resultArray;
    };

    //编辑原型表达式
    $scope.editExpression = function() {
        $scope.isSave = false;
    };

    $scope.cancelExpression = function() {
        $scope.info.expression = "";
        $scope.isSave = false;
    };

    $scope.sltExpression = function() {
        $scope.info.expression = "";
        switch($scope.selectedItem) {
            case "table": 
                $scope.info.expression = $scope.info.tableContent;
                break;
            case "tab-table": 
                $scope.info.expression = $scope.info.tabTableContent;
                break;
            case "form": 
                $scope.info.expression = $scope.info.formContent;
                break;
            case "text-form": 
                $scope.info.expression = $scope.info.textFormContent;
                break;
            case "flow": 
                $scope.info.expression = $scope.info.flowContent;
                break;
        }
    }
}]);